using CfgTable;
using IQIGame.Onigao.Framework;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class AnimatorGoActiveReset : StateMachineBehaviour
    {
        public bool isShowByParam;
        public ComparisonType comparisonType;
        public int paramId;
        public float targetValue;
        public bool showOnMeetCondition;
        public int[] hideObjectIndex;

        private EntityAnimEventRecevier _entityAnimDriver;

        // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (_entityAnimDriver == null)
            {
                _entityAnimDriver = animator.GetComponent<EntityAnimEventRecevier>();
                if (_entityAnimDriver == null)
                {
                    return;
                }
            }
            // 无论是否isShowByParam，离开状态的时候，都隐藏object
            for (int i = 0; i < hideObjectIndex.Length; i++)
            {
                var objIndex = hideObjectIndex[i];
                _entityAnimDriver.DeactiveObject(objIndex);
            }
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (!isShowByParam)
                return;
            if (_entityAnimDriver == null)
            {
                _entityAnimDriver = animator.GetComponent<EntityAnimEventRecevier>();
                if (_entityAnimDriver == null)
                {
                    return;
                }
            }

            float paramValue = GetParamValue(animator, paramId);
            bool meetCondition = CompareValue(paramValue, targetValue, comparisonType);
            bool isShow = (meetCondition && showOnMeetCondition) || (!meetCondition && !showOnMeetCondition);
            for (int i = 0; i < hideObjectIndex.Length; i++)
            {
                var objIndex = hideObjectIndex[i];
                if (isShow)
                {
                    _entityAnimDriver.ActiveObject(objIndex);
                }
                else
                {
                    _entityAnimDriver.DeactiveObject(objIndex);
                }
            }
        }

        private float GetParamValue(Animator animator, int paramId)
        {
            if (paramId == AnimationConst.Param_Speed)
            {
                return animator.GetFloat(paramId);
            }
            return 0;
        }

        private bool CompareValue(float currentValue, float specifiedValue, ComparisonType comparison)
        {
            switch (comparison)
            {
                case ComparisonType.GreaterEqual:
                    return currentValue >= specifiedValue;
                case ComparisonType.LessEqual:
                    return currentValue <= specifiedValue;
                case ComparisonType.NotEqual:
                    return currentValue != specifiedValue;
                case ComparisonType.Equal:
                    return currentValue == specifiedValue;
                case ComparisonType.GreaterThan:
                    return currentValue > specifiedValue;
                case ComparisonType.LessThan:
                    return currentValue < specifiedValue;
                default:
                    LogGame.LogError($"Unsupported comparison : {comparison}");
                    return false;
            }
        }
    }
}
